User Function MT20FOPOS      
Local nOpcA    := PARAMIXB[1]         
Local cQuery   := ''
Local cTexto   := ''
Local nProx    := 1
local aArea    := GetArea()        

                     
If nOpcA == 3
	cTexto := '************************'+CHR(13)+ chr(10)
	cTexto += 'Cod :'+SA2->A2_COD+ CHR(13)+ chr(10)
	cTexto += 'FUNCAO :'+Alltrim(FunName())+ CHR(13)+ chr(10)
	cTexto += '************************'+CHR(13)+ chr(10)
	cTexto += IIF(l020Auto,'Auto=SIM','Auto=NAO') +CHR(13)+ chr(10)
	
	IF l020Auto
		IF !(SUBSTR(SA2->A2_COD,1,1) $  'EAIN' )
			If Select("NUM_PRX") > 0
				NUM_PRX->(dbCloseArea())
			EndIf
			cQuery    =  " SELECT Max(CASE WHEN SUBSTRING(A2_COD,1,1)='L'
			cQuery    += "            THEN SUBSTRING(A2_COD,2,5)  "
			cQuery    += "            ELSE  ''       "
			cQuery    += "            END)   MAXNUM  "
			cQuery    += " FROM "+RetSqlName("SA2")
			cQuery := ChangeQuery(cQuery)
			dbUseArea(.T.,"TOPCONN",TcGenQry(,,cQuery),"NUM_PRX",.T.,.F.)
			dbSelectArea("NUM_PRX")
			
			NUM_PRX->(dbGoTop())
			RecLock("SA2", .F.)
			IF NUM_PRX->(Eof())
				SA2->A2_COD:= 'L00001'
			ELSE
				SA2->A2_COD:= 'L'+STRZERO(Val(NUM_PRX->MAXNUM)+1,5)
			ENDIF
			SA2->(msUnlock())
			cTexto += 'Cod :'+SA2->A2_COD+ CHR(13)+ chr(10)
		ENDIF
	ENDIF
	memowrite('For_logix.txt',cTexto)
ENDIF

RestArea(aArea)

Return